﻿#include "algworker.h"
#include <string>
#include <QDebug>


AlgWorker::AlgWorker(QObject *parent) : QObject(parent)
{
    for (int i = 0; i < 500; i++)
    {
        m_nNGorOK[i] = 44;
    }
}

AlgWorker::~AlgWorker()
{

}



void AlgWorker::Initialize()
{

}

void AlgWorker::start(CameraData m_cImageInfo)
{
    QTime time;
    time.start();
    HalconCpp::HObject img;
    img = m_cImageInfo.Hobj;
    int nSave = 0;
    int result = 0;
    result1 = 0;
    result2 = 0;
    CCDNum = m_cImageInfo.m_nIndex;
    hvCCDHandle = m_cImageInfo.hv_WindowHandle;
    switch (CCDNum) {
    case 1:
        nSave = m_swSaveImg.nCCD1;
        break;
    case 2:
        nSave = m_swSaveImg.nCCD2;
        break;
    case 3:
        nSave = m_swSaveImg.nCCD3;
        break;
    case 4:
        nSave = m_swSaveImg.nCCD4;
        break;
    case 5:
        nSave = m_swSaveImg.nCCD5;
        break;
    case 6:
        nSave = m_swSaveImg.nCCD6;
        break;
    case 7:
        nSave = m_swSaveImg.nCCD7;
        break;
    case 8:
        nSave = m_swSaveImg.nCCD8;
        break;
    default:
        break;
    }
    result = CCD_Alg(img);
    msg = QString("Time:%1s").arg(time.elapsed() / 1000.0);
    m_nNGorOK[m_nResult] = result;
    m_cImageInfo.m_nNGorOK = result;
    show(m_cImageInfo.Hobj, m_cImageInfo.hv_WindowHandle, result);

    if (nSave){
        emit sigSaveImgStart(m_cImageInfo);
    }
    emit sigFrameNum();

    if (m_nModel){
        return;
    }
    if (m_cImageInfo.m_nIndex == 8){
        emit sigCameraResult(m_nResult);
    }
    m_nResult++;
    if (m_nResult == 500)
    {
        m_nResult = 0;
    }
}

void AlgWorker::show(HalconCpp::HObject img, HalconCpp::HTuple hv_WindowHandle, int result)
{

    HalconCpp::HTuple w, h;
    HalconCpp::GetImageSize(img, &w, &h);
    HalconCpp::SetPart(hv_WindowHandle, 0, 0, h, w);
    HalconCpp::DispObj(img, hv_WindowHandle);
    HObject Rect, Contour;


    try
    {
        HalconCpp::GenRectangle1(&Rect, m_algPlace.x1, m_algPlace.y1, m_algPlace.x2, m_algPlace.y2);
        HalconCpp::GenContourRegionXld(Rect, &Contour, "border");
        HalconCpp::SetColor(hv_WindowHandle, "red");
        HalconCpp::DispObj(Contour, hv_WindowHandle);

        SetTposition(hv_WindowHandle, HTuple(30), HTuple(50));
        if (result != 22)
        {
            SetColor(hv_WindowHandle, "red");
            WriteString(hv_WindowHandle, "NG");
        }
        else
        {
            SetColor(hv_WindowHandle, "green");
            WriteString(hv_WindowHandle, "OK");
        }
        SetTposition(hv_WindowHandle, HTuple(90), HTuple(50));
        WriteString(hv_WindowHandle, msg.toStdString().c_str());
    }
    catch (HException ex)
    {
        qDebug() << "检测区域设置 错误";
    }

}


//M_Model 1测试高度，2测试长度，0全测量   CCD1 0  CCD2 CCD3 正方形 1  CCD4 0
//Direction 正方形 false   长方形true 梯形 true
//  OutPara   CCD1/CCD4 正面 1.宽度  2.平行度 角度 3.长度  4. 平行度   5.角度1  6.角度2  7.角度3 8.角度4
// Outpara  CCD3  侧面  1.厚度 2.平行度
// outpara  梯形 1.高度  2.角度1   3.角度2
int AlgWorker::CCD_Alg(HObject img)
{
    OutImg = img;
    HalconCpp::HTuple OutPara;

    float* pData;
    HalconCpp::HTuple len;
    double dLen;

    QList<bool> bFlag;
    QList<bool> bUD;
    float* fAlgNG;
    float* fAlgOK;

    float* fAlgUp;
    float* fAlgDown;

    // 缺陷
    switch (m_nAlgDefect) {
    case 1:
        m_defectRect.m_part2.x1 = m_algPlace.x1;
        m_defectRect.m_part2.y1 = m_algPlace.y1;
        m_defectRect.m_part2.x2 = m_algPlace.x2;
        m_defectRect.m_part2.y2 = m_algPlace.y2;
        m_defectRect.m_part2.hvCCDHandle = hvCCDHandle;

        m_defectRect.Outer = AlgSw1;
        m_defectRect.Inner = AlgSw2;

        result2 = m_defectRect.D_Rect(img, OutImg);



        m_dD_R_DataA.dExternal = m_defectRect.AreaValue[0].D();
        m_dD_R_DataA.dInterior = m_defectRect.AreaValue[1].D();




        // 统计数据
        if (!m_nModel){
            if (result2 == 0){
                m_nAlgOK.dD_R_DataA.dExternal += 1;
                m_nAlgOK.dD_R_DataA.dInterior += 1;
            }
            else{
                m_nAlgNG.dD_R_DataA.dExternal += 1;
                m_nAlgNG.dD_R_DataA.dInterior += 1;
            }
        }


        break;
    case 2:
        m_defectSector.m_part2.x1 = m_algPlace.x1;
        m_defectSector.m_part2.y1 = m_algPlace.y1;
        m_defectSector.m_part2.x2 = m_algPlace.x2;
        m_defectSector.m_part2.y2 = m_algPlace.y2;
        m_defectSector.m_part2.hvCCDHandle = hvCCDHandle;

        m_defectSector.Outer = AlgSw1;
        m_defectSector.Inner = AlgSw2;
        result2 = m_defectSector.D_Sector(img, ModelID, OutImg);

        m_dD_S_DataA.dExternal = m_defectSector.AreaValue[0].D();
        m_dD_S_DataA.dInterior = m_defectSector.AreaValue[1].D();


        // 统计数据
        if (!m_nModel){
            if (result2 == 0){
                m_nAlgOK.dD_S_DataA.dExternal += 1;
                m_nAlgOK.dD_S_DataA.dInterior += 1;
            }
            else{
                m_nAlgNG.dD_S_DataA.dExternal += 1;
                m_nAlgNG.dD_S_DataA.dInterior += 1;
            }
        }
        break;
    case 3:
        m_defectSide.m_part2.x1 = m_algPlace.x1;
        m_defectSide.m_part2.y1 = m_algPlace.y1;
        m_defectSide.m_part2.x2 = m_algPlace.x2;
        m_defectSide.m_part2.y2 = m_algPlace.y2;
        m_defectSide.m_part2.hvCCDHandle = hvCCDHandle;

        result2 = m_defectSide.D_Side(img, OutImg);
        break;
    case 4:
        m_defectFback.m_part2.x1 = m_algPlace.x1;
        m_defectFback.m_part2.y1 = m_algPlace.y1;
        m_defectFback.m_part2.x2 = m_algPlace.x2;
        m_defectFback.m_part2.y2 = m_algPlace.y2;
        m_defectFback.m_part2.hvCCDHandle = hvCCDHandle;

        result2 = m_defectFback.M_Fb(img, OutImg);
        break;
    default:
        result2 = 0;
        OutImg = img;
        break;
    }

    // 尺寸

    switch (m_nAlgRect) {
    case 1:
        // 尺寸：方形全检
        m_measureRect.m_part2.x1 = m_algPlace.x1;
        m_measureRect.m_part2.y1 = m_algPlace.y1;
        m_measureRect.m_part2.x2 = m_algPlace.x2;
        m_measureRect.m_part2.y2 = m_algPlace.y2;
        m_measureRect.m_part2.hvCCDHandle = hvCCDHandle;

        result1 = m_measureRect.M_Rect(img, 0, OutPara, OutImg);
        // 测量值
        pData = &m_dM_R_A.dWidth;
        for (int i = 0; i < 8; i++){
            *pData = OutPara[i].D();
            pData++;
        }
        // 统计数据
        if (!m_nModel){
            bFlag << m_measureRect.Flag1 << m_measureRect.Flag2
                  << m_measureRect.Flag3 << m_measureRect.Flag4
                  << m_measureRect.Flag5 << m_measureRect.Flag6
                  << m_measureRect.Flag7 << m_measureRect.Flag8;

            fAlgNG = &m_nAlgNG.dM_R_DataA.dWidth;
            fAlgOK = &m_nAlgOK.dM_R_DataA.dWidth;


            bUD << m_measureRect.Flag11 << m_measureRect.Flag22
                << m_measureRect.Flag33 << m_measureRect.Flag44
                << m_measureRect.Flag55 << m_measureRect.Flag66
                << m_measureRect.Flag77 << m_measureRect.Flag88;

            fAlgUp = &m_nAlgUp.dM_R_DataA.dWidth;
            fAlgDown = &m_nAlgDown.dM_R_DataA.dWidth;

            for (int i = 0; i < 8; i++)
            {
                if (bFlag[i])
                {
                    *fAlgOK += 1;
                }
                else
                {
                    if (result1 == 1){
                        *fAlgNG += 1;
                        if (bUD[i])
                        {
                            *fAlgUp += 1;
                        }
                        else{
                            *fAlgDown += 1;
                        }
                    }

                }
                fAlgNG++;
                fAlgOK++;
                fAlgUp++;
                fAlgDown++;
            }
        }
        break;
    case 2:

        // 尺寸：梯形
        result1 = m_measureTrapezoid.M_Trapezoid(img, OutPara, OutImg);
        pData = &m_dM_T_A.dHeight;
        for (int i = 0; i < 4; i++){
            *pData = OutPara[i].D();
            pData++;
        }


        // 统计数据
        if (!m_nModel){
            bFlag << m_measureTrapezoid.Flag1 << m_measureTrapezoid.Flag2
                  << m_measureTrapezoid.Flag3 << m_measureTrapezoid.Flag4;

            fAlgNG = &m_nAlgNG.dM_T_DataA.dHeight;
            fAlgOK = &m_nAlgOK.dM_T_DataA.dHeight;
            for (int i = 0; i < 4; i++)
            {
                if (bFlag[i])
                {
                    *fAlgOK += 1;
                }
                else
                {
                    if (result1 == 1){
                        *fAlgNG += 1;
                    }


                }
                fAlgNG++;
                fAlgOK++;
            }
        }
        break;
    case 3:
        // 尺寸：侧面
        result1 = m_measureSide.M_Side(img, OutPara, OutImg);
        m_dM_R_B.dThickness = OutPara[0].D();

        // 统计数据
        if (!m_nModel){
            bFlag << m_measureSide.Flag1;

            fAlgNG = &m_nAlgNG.dM_R_DataB.dThickness;
            fAlgOK = &m_nAlgOK.dM_R_DataB.dThickness;



            bUD << m_measureSide.Flag11;

            fAlgUp = &m_nAlgUp.dM_R_DataB.dThickness;
            fAlgDown = &m_nAlgDown.dM_R_DataB.dThickness;

            for (int i = 0; i < 1; i++)
            {
                if (bFlag[i])
                {
                    *fAlgOK += 1;
                }
                else
                {
                    if (result1 == 1){
                        *fAlgNG += 1;
                        if (bUD[i])
                        {
                            *fAlgUp += 1;
                        }
                        else{
                            *fAlgDown += 1;
                        }
                    }
                }
                fAlgNG++;
                fAlgOK++;
                fAlgUp++;
                fAlgDown++;
            }
        }

        break;
    case 4:
        // 尺寸：异形正面
        result1 = m_measureTS.M_TS(img, ModelID, OutPara, OutImg);
        pData = &m_dM_TS_A.dHeight;


        for (int i = 0; i < 2; i++){
            *pData = OutPara[i].D();
            pData++;
        }

        // 统计数据
        if (!m_nModel){
            bFlag << m_measureTS.Flag1 << m_measureTS.Flag2;

            fAlgNG = &m_nAlgNG.dM_TS_DataA.dHeight;
            fAlgOK = &m_nAlgOK.dM_TS_DataA.dHeight;



            bUD << m_measureTS.Flag11 << m_measureTS.Flag22;

            fAlgUp = &m_nAlgUp.dM_TS_DataA.dHeight;
            fAlgDown = &m_nAlgDown.dM_TS_DataA.dHeight;

            for (int i = 0; i < 2; i++)
            {
                if (bFlag[i])
                {
                    *fAlgOK += 1;
                }
                else
                {
                    if (result1 == 1){
                        *fAlgNG += 1;
                        if (bUD[i])
                        {
                            *fAlgUp += 1;
                        }
                        else{
                            *fAlgDown += 1;
                        }
                    }
                }
                fAlgNG++;
                fAlgOK++;
                fAlgUp++;
                fAlgDown++;
            }
        }


        break;
    case 5:
        // 尺寸：异形侧面
        result1 = m_measureTSS.M_TSS(img, OutPara, OutImg);
        m_dM_TSS_A.dThickness = OutPara[0].D();




        // 统计数据
        if (!m_nModel){
            bFlag << m_measureTSS.Flag;

            fAlgNG = &m_nAlgNG.dM_TSS_DataA.dThickness;
            fAlgOK = &m_nAlgOK.dM_TSS_DataA.dThickness;



            bUD << m_measureTSS.Flag11;

            fAlgUp = &m_nAlgUp.dM_TSS_DataA.dThickness;
            fAlgDown = &m_nAlgDown.dM_TSS_DataA.dThickness;

            for (int i = 0; i < 2; i++)
            {
                if (bFlag[i])
                {
                    *fAlgOK += 1;
                }
                else
                {
                    if (result1 == 1){
                        *fAlgNG += 1;
                        if (bUD[i])
                        {
                            *fAlgUp += 1;
                        }
                        else{
                            *fAlgDown += 1;
                        }
                    }

                }
                fAlgNG++;
                fAlgOK++;
                fAlgUp++;
                fAlgDown++;
            }
        }

        break;
    case 6:
        result1 = m_JudgeMateriel.J_Materiel(img, ModelID);
        break;

    case 7:

        // 尺寸：扇形
        m_measureSector.m_part2.x1 = m_algPlace.x1;
        m_measureSector.m_part2.y1 = m_algPlace.y1;
        m_measureSector.m_part2.x2 = m_algPlace.x2;
        m_measureSector.m_part2.y2 = m_algPlace.y2;
        m_measureSector.m_part2.hvCCDHandle = hvCCDHandle;

        result1 = m_measureSector.M_Sector(img, ModelID, OutPara, OutImg);

        HalconCpp::TupleLength(OutPara, &len);
        dLen = len.D();
        pData = &m_dM_Se_A.dEdge1;
        for (int i = 0; i < 6; i++){
            *pData = OutPara[i].D();
            pData++;
        }


        // 统计数据
        if (!m_nModel){

            bFlag << m_measureSector.Flag1 << m_measureSector.Flag2
                  << m_measureSector.Flag3 << m_measureSector.Flag4
                  << m_measureSector.Flag5 << m_measureSector.Flag6;

            fAlgNG = &m_nAlgNG.dM_Se_DataA.dEdge1;
            fAlgOK = &m_nAlgOK.dM_Se_DataA.dEdge1;


            bUD << m_measureSector.Flag11 << m_measureSector.Flag22
                << m_measureSector.Flag33 << m_measureSector.Flag44
                << m_measureSector.Flag55 << m_measureSector.Flag66;

            fAlgUp = &m_nAlgUp.dM_Se_DataA.dEdge1;
            fAlgDown = &m_nAlgDown.dM_Se_DataA.dEdge1;

            for (int i = 0; i < 6; i++)
            {
                if (bFlag[i])
                {
                    *fAlgOK += 1;
                }
                else
                {
                    if (result1 == 1){
                        *fAlgNG += 1;
                        if (bUD[i])
                        {
                            *fAlgUp += 1;
                        }
                        else{
                            *fAlgDown += 1;
                        }
                    }

                }
                fAlgNG++;
                fAlgOK++;
                fAlgUp++;
                fAlgDown++;
            }
        }

        break;

    case 8:

        // 尺寸：子弹头
        m_measureBullet.m_part2.x1 = m_algPlace.x1;
        m_measureBullet.m_part2.y1 = m_algPlace.y1;
        m_measureBullet.m_part2.x2 = m_algPlace.x2;
        m_measureBullet.m_part2.y2 = m_algPlace.y2;
        m_measureBullet.m_part2.hvCCDHandle = hvCCDHandle;

        result1 = m_measureBullet.M_Bullet(img, OutPara, OutImg);


        pData = &m_dM_Bullet_A.dWidth;
        for (int i = 0; i < 4; i++){
            *pData = OutPara[i].D();
            pData++;
        }


        // 统计数据
        if (!m_nModel){

            bFlag << m_measureBullet.Flag1 << m_measureBullet.Flag2
                  << m_measureBullet.Flag3 << m_measureBullet.Flag4;

            fAlgNG = &m_nAlgNG.dM_Bullet_DataA.dWidth;
            fAlgOK = &m_nAlgOK.dM_Bullet_DataA.dWidth;


            bUD << m_measureBullet.Flag11 << m_measureBullet.Flag22
                << m_measureBullet.Flag33 << m_measureBullet.Flag44;

            fAlgUp = &m_nAlgUp.dM_Bullet_DataA.dWidth;
            fAlgDown = &m_nAlgDown.dM_Bullet_DataA.dWidth;

            for (int i = 0; i < 6; i++)
            {
                if (bFlag[i])
                {
                    *fAlgOK += 1;
                }
                else
                {
                    if (result1 == 1){
                        *fAlgNG += 1;
                        if (bUD[i])
                        {
                            *fAlgUp += 1;
                        }
                        else{
                            *fAlgDown += 1;
                        }
                    }

                }
                fAlgNG++;
                fAlgOK++;
                fAlgUp++;
                fAlgDown++;
            }
        }

        break;
    default:
        result1 = 0;
        break;
    }



    if (!m_nModel){
        if (result1 == 2 || result1 == -1){
            m_nRectAbn++;
        }
        if (result2 == 2 || result2 == -1){
            m_nDefectAbn++;
        }
    }





    switch (result1) {
    case -1://混料
        result1 = 1;
        break;
    case 0://正常
        result1 = 2;
        break;
    case 1://错误
        result1 = 3;
        break;
    case 2://异常
        result1 = 4;
        break;
    default:
        result1 = 4;
        break;
    }




    switch (result2) {
    case -1://混料
        result2 = 1;
        break;
    case 0://正常
        result2 = 2;
        break;
    case 1://错误
        result2 = 3;
        break;
    case 2://异常
        result2 = 4;
        break;
    default:
        result2 = 4;
        break;
    }


    result2 = result2 * 10;
    return result1 + result2;
}
